class Observer {
    constructor(state) {
        this.state = state
        this.arr = []
    }
    attach(s){
        this.arr.push(s);
    }
    setState(newVal) {
        this.state = newVal;
        this.arr.forEach(s=>s.update(newVal))
    }
}

class Subject{
    constructor(name){
        this.name=name
    }
    update(newVal){
        console.log(`${this.name}观察到了宝宝的状态变化：${newVal}`);
        
    }
}

var baby = new Observer('不开心');
var father = new Subject('父亲');
var mama = new Subject('妈妈');

baby.attach(father)
baby.attach(mama)
baby.setState('心情愉快')
setTimeout(() => {
    console.log('过了一阵');
    
    baby.setState('摔倒了，心情沮丧')
}, 1000);